home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DTP / DTP_TEX / H220.ZIP / ITRNS211.ZIP / SRC / ITRANS.C < prev    next >
C/C++ Source or Header  |  1991-10-14  |  14KB  |  469 lines

  1. /*
  2.  *========================================================================== 
  3.  * Copyright 1991 Avinash Chopde, All Rights Reserved.
  4.  *
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of Avinash Chopde not be used in
  10.  * advertising or publicity pertaining to distribution of the software
  11.  * without specific, written prior permission.
  12.  * Avinash Chopde makes no representations about the suitability of this
  13.  * software for any purpose.
  14.  * It is provided "as is" without express or implied warranty.
  15.  *
  16.  * AVINASH CHOPDE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  18.  * IN NO EVENT SHALL AVINASH CHOPDE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  20.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  21.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  *
  24.  * Author:  Avinash Chopde, 1991
  25.  *
  26.  */
  27.  
  28. static char S_RCSID[] = "$Header: e:/itrans/src/rcs/itrans.c 1.9 91/10/14 20:18:06 avinash Exp $";
  29.  
  30. #include "itrans.h"
  31. #include "ifm.h"
  32.  
  33. static char S_copyright[] = "\
  34. %%\n\
  35. %% ----------------------------------------------------------------\n\
  36. %% Output from:\n\
  37. %% itrans %s, Copyright 1991 Avinash Chopde, All Rights Reserved.\n\
  38. %% ----------------------------------------------------------------\n\
  39. %%\n\
  40. ";
  41.  
  42. /* =================================================================== */
  43. #ifdef TCC
  44. /* Under Turbo-C++, declare the stacklen to be larger */
  45. extern unsigned _stklen = 7000U;
  46. #endif
  47. /* =================================================================== */
  48.  
  49. int G_verbose;
  50.  
  51. int G_currfontsize;
  52. int G_psout; /* if TRUE produce PostScript output, else TeX output */
  53.  
  54. static allfonts_t    S_allfonts;
  55. static lang_t        S_langs[LANGS_MAX];
  56.  
  57. font_t*            G_current_font=NULL; /*GLOBAL*/
  58.  
  59.  
  60. static void S_usage(FILE*);
  61.  
  62. /* =================================================================== */
  63. void cc_toany(letter_t dlet, font_t* mfontptr, char* comm, int* wid)
  64. {
  65.     int i, x, y;
  66.     static comp_unit_t    cus[100]; /* static just to over the PC's 
  67.         * stack size limits...!
  68.         */
  69.  
  70.     i = make_letter(mfontptr, dlet, cus, 100, &x, &y, wid);
  71.  
  72.     if (i > 0) {
  73.  
  74.     if (G_psout) cus_to_ps(cus, G_currfontsize, comm);
  75.     else cus_to_tex(cus, G_currfontsize, comm);
  76.  
  77.     }
  78.  
  79. } /* cc_toany() */
  80.  
  81. /* =================================================================== */
  82.  
  83. process_letter(letter_t in)
  84. {
  85.     static char comm[CONVWORD_LEN];
  86.     /* must be large enough to store one word -
  87.      * which in TeX can get very large..what with raise boxes, \char defs etc
  88.      * made it static since it is currently 8K bytes, and on the PC stack
  89.      * space is at a premium...
  90.      */
  91.  
  92.     int c1, c2, v, wid;
  93.     /* Check for output validity */
  94.     if (G_psout && G_current_font->prop == TEX_FONT) {
  95.     fprintf(stderr, "Font %s is for TeX output only - cannot do PostScript out\n", G_current_font->fname);
  96.  
  97.     /***** EXITS HERE *****/
  98.     exit(1);
  99.     /* return FALSE; */
  100.     }
  101.  
  102.     /*
  103.     fprintf(stderr, ":offset is %d in.v %d, in.c1 %d, in.c2 %d\n", OFFSET_TOK,
  104.                 in.v, in.c1, in.c2);
  105.     */
  106.  
  107.     if (in.v < OFFSET_TOK && in.v != HALF_FORM && in.v != IMPLICIT_FORM) {
  108.         fprintf(stderr,"illegal vowel val (c[0] is %d) - will use 'a' form\n", in.cons[0]);
  109.     }
  110.  
  111.  
  112.     switch(in.type) {
  113.     case VOWEL_TYPE:
  114.     v = _I_(in.v);
  115. #ifdef DEBUG
  116. fprintf(stderr, "process_letter: vowel %d\n", v);
  117. #endif /*DEBUG*/
  118.     cc_toany(in, G_current_font, comm, &wid);
  119.  
  120.     break;
  121.     case SPECIAL_TYPE:
  122.     c1 = _I_(in.cons[0]);
  123.     in.v = IMPLICIT_FORM; /* all specials printed in implicit form */
  124.  
  125. #ifdef DEBUG
  126. fprintf(stderr, "process_letter: special %d\n", c1);
  127. #endif /*DEBUG*/
  128.     
  129.     cc_toany(in, G_current_font, comm, &wid);
  130.  
  131.     break;
  132.     case CONSONANT_SINGLE_TYPE:
  133.     c1 = _I_(in.cons[0]);
  134.     v = _I_(in.v);
  135. #ifdef DEBUG
  136. fprintf(stderr, "process_letter: cons single %d vowel %d\n", c1, v);
  137. #endif /*DEBUG*/
  138.     
  139.     cc_toany(in, G_current_font, comm, &wid);
  140.  
  141.     break;
  142.  
  143.     case CONSONANT_DOUBLE_TYPE:
  144.     c1 = _I_(in.cons[0]);
  145.     c2 = _I_(in.cons[1]);
  146.     v = _I_(in.v);
  147.  
  148. #ifdef DEBUG
  149. fprintf(stderr, "process_letter: cons double %d %d vowel %d\n", c1, c2, v);
  150. #endif /*DEBUG*/
  151.     cc_toany(in, G_current_font, comm, &wid);
  152.  
  153.     break;
  154.  
  155.     case CONSONANT_MANY_TYPE:
  156.     c1 = _I_(in.cons[0]);
  157.     c2 = _I_(in.cons[1]);
  158.     v = _I_(in.v);
  159.  
  160. #ifdef DEBUG
  161. fprintf(stderr, "process_letter: cons many %d %d vowel %d\n", c1, c2, v);
  162. #endif /*DEBUG*/
  163.     cc_toany(in, G_current_font, comm, &wid);
  164.  
  165.     break;
  166.     }
  167.  
  168.     fputs(comm, stdout);
  169.  
  170.     if (G_psout) printf("\n");
  171.  
  172.     return TRUE;
  173. }
  174.  
  175. /* =================================================================== */
  176. int main(int argc, char *argv[])
  177. {
  178.     static char ifmfname[NAMELEN];
  179.     static char ifmcmd[NAMELEN];
  180.         /* static just to overcome the PC's stack size limitations */
  181.  
  182.     extern int yydebug;
  183.     int ch;
  184.     int psout;
  185.     extern char * optarg;
  186.     
  187. #ifdef sgi
  188.     mallopt(M_DEBUG, TRUE);
  189. #endif
  190.  
  191.     for (ch = 0; ch < FONTS_MAX; ch++) S_allfonts[ch] = NULL;
  192.  
  193.     ifmfname[0] = '\0';
  194.     G_currfontsize = 30;
  195.     G_verbose = 0;
  196.     psout = FALSE; /* by default, produce TeX output */
  197.  
  198.     /* --- Collect Args */
  199.     strcpy(ifmfname, DEVN_IFM_FILE);
  200.  
  201.     while ((ch = getopt(argc, argv, "hHPI:f:o:i:v")) != EOF) {
  202.     switch (ch) {
  203.     case 'h':
  204.     case 'H':
  205.        S_usage(stderr);
  206.        exit(0);
  207.        break;
  208.     case 'v':
  209.        G_verbose++;
  210.        break;
  211.     case 'P':
  212.        psout = TRUE;
  213.        break;
  214.     case 'I':
  215.        strcpy(ifmfname, optarg);
  216.        break;
  217.     case 'f':
  218.        sscanf(optarg, "%d", &G_currfontsize);
  219.        break;
  220.     case 'i':
  221.        if (!freopen(optarg, "r", stdin)) {
  222.            fprintf(stderr,"Error: Could not open %s for reading\n", optarg);
  223.            exit(1);
  224.        }
  225.        break;
  226.     case 'o':
  227.        if (!freopen(optarg, "w", stdout)) {
  228.            fprintf(stderr,"Error: Could not open %s for writing\n", optarg);
  229.            exit(1);
  230.        }
  231.        break;
  232.     default:
  233.        S_usage(stderr);
  234.        break;
  235.     } /* switch */
  236.     }
  237.     
  238.     G_psout = psout;
  239.  
  240.     for (ch = 0; ch < FONTS_MAX; ch++) {
  241.     S_allfonts[ch] = NULL;
  242.     }
  243.     init_langs(S_langs, LANGS_MAX);
  244.  
  245.     /* construct a line with a ifm command, so that
  246.      * the find_load_ifm function in lang.c can handle the loading...
  247.      */
  248.     sprintf(ifmcmd, "\\indianifm=%s", ifmfname);
  249.  
  250.     /* load in the default ifm file... */
  251.  
  252.     if (!find_load_ifm(&S_langs[0], S_allfonts, ifmcmd)) {
  253.      fprintf(stderr, "Warning: could not load default IFM %s (hope you don't need it..ERROR otherwise..)\n", ifmfname);
  254.     }
  255.  
  256.     G_current_font = S_langs[0].curr_font; /* default font */
  257.  
  258.     /* dump_font(S_allfonts[0], stderr); */
  259.  
  260.     yydebug = 0;
  261.     if (psout) {
  262.     printf("%%!PS\n%% *** Need files devnac.ps and itrans.pro before this line..\n");
  263.     fprintf(stderr, "** itrans, version %s (PostScript Interface)\n", ITRANS_VER);
  264.     } else {
  265.     fprintf(stderr, "** itrans, version %s (TeX Interface)\n", ITRANS_VER);
  266.     }
  267.  
  268.     printf(S_copyright, ITRANS_VER);
  269.  
  270.     if (psout) {
  271.     outps_start(G_currfontsize);
  272.     printf("/DevnacPlain findfont %s scalefont setfont\n", EMSIZE);
  273.     printf("/BASEFONTNAME /DevnacPlain def\n");
  274.     } else {
  275.     outtex_start();
  276.     }
  277.  
  278.     yyparse();
  279.  
  280.     if (psout) outps_end();
  281.     else outtex_end();
  282.  
  283.     return 0;
  284. }
  285. /* =================================================================== */
  286. process_newline()
  287. {
  288.     if (G_psout) printf("%%\nnewline\n");
  289.     else printf("\n");
  290.     return TRUE;
  291. }
  292. /* =================================================================== */
  293. process_space()
  294. {
  295.     if (G_psout) printf("( ) show ");
  296.     else printf(" ");
  297.     return TRUE;
  298. }
  299. /* =================================================================== */
  300. process_otherchar(int c)
  301. {
  302.     if (G_psout) {
  303.     if (c != '{' && c != '}') printf("(%c) show ", c);
  304.     }
  305.     else putchar(c);
  306.  
  307.     if (!ispunct(c) && !isdigit(c) && !isspace(c))  {
  308.         fprintf(stderr, "Note (around line %d): character <%c> (dec %d) unrecognized by lexer, echoing to output\n", G_lineno, c, c);
  309.     }
  310.     return TRUE;
  311. }
  312. /* =================================================================== */
  313. process_init_word(char* word)
  314. {
  315.     printf("%% previous word:<%s>\n", word);
  316.     return TRUE;
  317. }
  318. /* =================================================================== */
  319. process_end_word(char* word)
  320. {
  321.     /*
  322.     Can't print a comment here, since in TeX everything following 
  323.     also becomes a comment, even on the next line.
  324.     And, since the space following the word may not have been
  325.     seen yet, it gets eaten up if a comment is printed here,
  326.     therefore, the word is printed in the function process_init_word()
  327.     instead..clumsy..
  328.  
  329.     printf("%%\n%% Just saw word <%s>\n", word);
  330.     */
  331.     word[0] = word[0]; /* to prevent unused warning ... */
  332.     return TRUE;
  333. }
  334. /* =================================================================== */
  335. process_command(char in[])
  336. {
  337.     char com[1024], *cptr;
  338.     float amt;
  339.  
  340.     strcpy(com, in);
  341.  
  342.     if (!G_psout) printf("\\%s", in); /* TeX, just copy commands */
  343.     else { /* is postscript out */
  344.  
  345.         if (!strncmp(com, "kern", 4)) { /* check if kern command */
  346.         cptr = &com[4];
  347.         if (sscanf(cptr, "%f", &amt) != 1) {
  348.             fprintf(stderr, "illegal kern amount <%f>\n", amt);
  349.             return FALSE;
  350.         }
  351.         printf("%s %.3f mul 0.0 rmoveto %% output of a kern command\n",
  352.                 EMSIZE, -amt);
  353.         } else {
  354.             fprintf(stderr, "PostScript out: command not implemented yet %s\n", in);
  355.         }
  356.     } /* else: is postscript out*/
  357.  
  358.     return TRUE;
  359.  
  360. }
  361. /* =================================================================== */
  362. void process_istart(int lang_tok)
  363. {
  364.  
  365.     int i = lang_tok - ILANG_TOK;
  366.  
  367.     if (!G_psout) printf("{"); /* provide an enclosing scope for indian
  368.                     * language text stuff for TeX output
  369.                 */
  370.  
  371.     /* set the correct IFM file, and echo out font settings, if any.. */
  372.     G_current_font = S_langs[i].curr_font;
  373.     if (!G_current_font) {
  374.     if (G_verbose)  {
  375.         fprintf(stderr, "Warning: No IFM file specified for language %s (line %d)\n", 
  376.             S_langs[i].name, G_lineno);
  377.         fprintf(stderr, "Warning: Will use the default IFM file\n");
  378.     }
  379.  
  380.     i = 0;
  381.     G_current_font = S_langs[i].curr_font;
  382.         if (!G_current_font) {
  383.         fprintf(stderr, "Error: Could not load even the default IFM file!\n");
  384.         exit(10);
  385.         }
  386.     }
  387.  
  388.     /* echo out font settings */
  389.     if (S_langs[i].curr_fontcmd) printf("%s", S_langs[i].curr_fontcmd);
  390. }
  391. /* =================================================================== */
  392. void process_iend()
  393. {
  394.     if (!G_psout) printf("}"); /* close the enclosing scope for indian
  395.                     * language text stuff for TeX output
  396.                 * (see process_istart())
  397.                 */
  398.  
  399.     printf("%% End of Indian Language\n");
  400. }
  401. /* =================================================================== */
  402. int setifm(int lang_tok, char* ifmtok)
  403. {
  404.     int i = lang_tok - ILANG_TOK;
  405. #ifdef DEBUG
  406.     fprintf(stderr, "loading in language %d at line %d %s\n", lang_tok,
  407.             G_lineno, ifmtok);
  408. #endif /*DEBUG*/
  409.  
  410.     if (!find_load_ifm(&S_langs[i], S_allfonts, ifmtok)) {
  411.      fprintf(stderr, "Warning: could not load in IFM file (line %d, %s) (hope you don't need it..ERROR otherwise..)\n", G_lineno, ifmtok);
  412.      return FALSE;
  413.     }
  414.  
  415.     return TRUE;
  416. }
  417. /* =================================================================== */
  418. int setfontcmd(int lang_tok, char ifmtok[])
  419. {
  420.     int i = lang_tok - ILANG_TOK;
  421.     char* eqp;
  422.  
  423. #ifdef DEBUG
  424.     fprintf(stderr, "setting font for language %d at line %d %s\n", lang_tok,
  425.             G_lineno, ifmtok);
  426. #endif /*DEBUG*/
  427.  
  428.     if (S_langs[i].curr_fontcmd) free(S_langs[i].curr_fontcmd);
  429.     S_langs[i].curr_fontcmd = NULL;
  430.  
  431.     eqp = strchr(ifmtok, '=');
  432.     if (eqp == NULL) {
  433.         fprintf(stderr, "Illegal Indian Font name line: %s (line %d) \n",
  434.             ifmtok, G_lineno);
  435.         return FALSE;
  436.     }
  437.  
  438.     eqp ++;
  439.  
  440.     if (strlen(eqp) > 0) {
  441.         S_langs[i].curr_fontcmd = (char*) malloc(sizeof(char)*(strlen(eqp)+1));
  442.  
  443.         /* eat up all white space around it..*/
  444.         sprintf(S_langs[i].curr_fontcmd, "%s", eqp);
  445.  
  446.     if (G_verbose > 1)
  447.           fprintf(stderr,"Set font command to: (%s)\n",S_langs[i].curr_fontcmd);
  448.  
  449.     /* in TeX mode, inhibit hyphenation... */
  450.     if (!G_psout) printf(
  451.      "\\hyphenchar%s=-1 %% itrans:do not hyphenate words using this font\n",
  452.              S_langs[i].curr_fontcmd);
  453.  
  454.     }
  455.  
  456.     return TRUE;
  457. }
  458. /* =================================================================== */
  459. static void S_usage(FILE *fp)
  460. {
  461.     fprintf(fp,"Usage: itrans [-P [-f <fontsize>]] [-I <ind.ifm>] [-i <infile>] [-o <outfile>] [-h|H]\n");
  462.     fprintf(fp,"            -P for PostScript output, TeX output by default\n");
  463.     fprintf(fp,"            -I specifies the Indian Metrics (IFM) file name\n");
  464.     fprintf(fp,"            -i input file name, stdin by default\n");
  465.     fprintf(fp,"            -o output file name, stdout by default\n");
  466.     fprintf(fp,"            -h|H for this message\n");
  467. }
  468. /* =================================================================== */
  469.